<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/9 0009
 * Time: 16:04
 */

namespace app\admin\controller;


use app\admin\model\Lab;
use app\admin\model\Tool as ToolModel;
use think\Controller;
use think\Session;
use think\Db;
use app\admin\validate\Tool as ToolVa;
class AdminTool extends Controller
{
    //中间件登录限制
    protected $middleware = [
        'AdminCheck'
    ];
    //器具列表
    public function tool_list(){
        $tool=new ToolModel();
        $tools=$tool->paginate(10);
        $this->assign('tools',$tools);
        return $this->fetch();
    }
    //器具添加
    public function tool_add(){
        if ($this->request->isAjax()){
            $data=input('post.');
            $tool=new ToolModel();
            $validate=new ToolVa();
            $va=$validate->check($data);
            if ($va==true){
                $tool_add=$tool->save([
                    'name'=>$data['name'],
                    'num' =>$data['num'],
                    'lab_id'=>$data['lab_id'],
                    'times'=>0,
                    'type'  =>$data['type'],
                    'statu' =>0
                ]);
                if ($tool_add){
                    $msg=$this->msg(200,'添加成功');
                }else{
                    $msg=$this->msg(400,'添加失败');
                }
            }else{
                $msg=$this->msg(400,$validate->getError());
            }

            return $msg;
        }
        $lab=new Lab();
        if (empty(session('admin'))){
            $session=\session('teacher')['id'];
            $labs=$lab->where('teacher_id',$session)->select();
        }else{
            $labs=$lab->select();
        }
        $this->assign('labs',$labs);
        return $this->fetch();
    }
    //器具更新
    public function tool_update($id){
        $tool=new ToolModel();
        $lab=new Lab();
        if ($this->request->isAjax()){
            $data=input('post.');
            $validate=new ToolVa();
            $va=$validate->check($data);
            if ($va==true){
                $update=$tool->save([
                    'name'=>$data['name'],
                    'num' =>$data['num'],
                    'type'=>$data['type'],
                    'lab_id'=>$data['lab_id']
                ],['id'=>$data['id']]);
                if ($update){
                    $msg=$this->msg(200,'更新成功');
                }else{
                    $msg=$this->msg(400,'更新失败');
                }
            }else{
                $msg=$this->msg(400,$validate->getError());
            }

             return $msg;
        }
        $tool=$tool->where('id',$id)->find();
        if (empty(session('admin'))){
            $session=\session('teacher')['id'];
            $labs=$lab->where('teacher_id',$session)->select();
        }else{
            $labs=$lab->select();
        }
        $this->assign(
            [
                'labs'=>$labs,
                'tool'=>$tool
            ]);
        return $this->fetch();
    }
    //器具删除
    public function tool_dele(){
        $id=input('post.id');
        $tool=ToolModel::destroy($id);
        if ($tool){
            $msg=$this->msg(200,'删除成功');
        }else{
            $msg=$this->msg(400,'删除失败');
        }
        return $msg;
    }
    //设为损坏
    public function tool_disable(){
        $id=input('post.id');
        $tool=Db::table('tool')->where('id',$id)->update(['statu'=>2]);
        if ($tool) {
            $msg=$this->msg(200,'设置成功');
        }else{
            $msg=$this->msg(400,'设置失败');
        }
        return $msg;
    }
    //我的器具
    public function tool_teacher_list(){
        $tools=ToolModel::hasWhere('lab',[
            'teacher_id'=>session('teacher')['id']
        ])->paginate(10);
        $this->assign('tools',$tools);
        return $this->fetch();
    }
    //信息
    public function msg($statu,$msg){
        return [
          'statu'=>$statu,
          'msg'  =>$msg
        ];
    }
}